<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\lib\exception\BaseException;
use app\model\Products;
use app\model\ProductsGroup;
use app\model\ProductsType;

class Product extends BaseController
{
    public function index($limit=10)
    {
        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $type = request()->param('type');
        $school = request()->param('school_id');
        $supplier_id = request()->param('supplier_id');

        $data = Products::with(['cate', 'pp', 'dw', 'gys','stock'=>function($q)use($school){
            $q->where('school_id',$school);
        }])
            ->when($name, function ($query) use ($name) {
            $query->where('name', 'like', "%$name%");
        })
            ->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })
            ->when($type, function ($query) use ($type) {

                $query->where('products_type_id', $type);
            })
            ->when($supplier_id, function ($query) use ($supplier_id) {

                $query->where('supplier_id', $supplier_id);
            })
            ->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->order('id', 'desc')->paginate($limit);

        foreach ($data as &$item){
        $item['sum']=\app\model\Warehouse::where('product_id',$item['id'])->sum('num');
    }
        return $this->success($data);
    }

    public function create()
    {
        $params = request()->param();
        $params['number'] = getNumber('產品');
        Products::create($params);
        return $this->success();
    }

    public function update($id)
    {
        Products::where('id',$id)->update(request()->param());

        return $this->success();
    }

    public function delete($id)
    {
        Products::where('id','in',$id)->delete();
        return $this->success();
    }

    public function conduct()
    {
        $model=new Products();
        $model->saveAll(request()->param());
        return $this->success();
    }

    public function updateStatus($id)
    {
        Products::where('id','in',$id)->update(['status'=>-1]);
        return $this->success();
    }

    public function excelCreate()
    {

        $data = json_decode(request()->param('data'),true);
        foreach ($data as $k=>$v){
           $producttype= ProductsType::where('name',$v['products_type'])->find();
           if(empty($producttype)){
               throw new BaseException(['msg'=>'第'.($k+1).'列沒有該產品分類']);
           }
            $brand=\app\model\Brand::where('name',$v['brand'])->find();
           if(empty($brand)){
               throw new BaseException(['msg'=>'第'.($k+1).'列沒有該產品品牌']);
           }
           $unit=\app\model\Unit::where('name',$v['unit'])->find();
            if(empty($unit)){
                throw new BaseException(['msg'=>'第'.($k+1).'列沒有該產品單位']);
            }
            $supplier=\app\model\Supplier::where('name',$v['supplier'])->find();
            if(empty($supplier)){
                throw new BaseException(['msg'=>'第'.($k+1).'列沒有該供應商']);
            }
            Products::create([
                'name'=>$v['name'],
                'products_type_id'=>$producttype->id,
                'number'=>getNumber('產品'),
                'brand_id'=>$brand->id,
                'unit_id'=>$unit->id,
                'barcode'=>$v['barcode'],
                'type'=>$v['type'],
                'price'=>$v['price'],
                'cost'=>$v['cost'],
                'remark'=>$v['remark']??'',
                'safety'=>$v['safety']?? 10,
                'supplier_id'=>$supplier->id,
                'maintain'=>$v['maintain'],
                'introduce'=>$v['introduce']??'',
            ]);
            return $this->success();
        }
    }

    public function derive($limit=15)
    {
       //1产品，2产品组合，3课程
        $status=\request()->param('status');
        $type=\request()->param('type');
        if(!empty($type) && $type==1){
          $data=  Products::with(['cate','pp','dw'])->when($status,function ($query) use($status){
                $query->where('status',$status);
            })->paginate($limit);
        }elseif (!empty($type) && $type==2){
          $data=  ProductsGroup::with(['cate','pp','dw'])->when($status,function ($query) use($status){
                $query->where('status',$status);
            })->paginate($limit);
        }elseif (!empty($type) && $type==3){
          $data=  \app\model\Course::with(['cate'])->when($status,function ($query) use($status){
                $query->where('status',$status);
            })->paginate($limit);
        }else{
            $data=  Products::with(['cate','pp','dw'])->when($status,function ($query) use($status){
                $query->where('status',$status);
            })->paginate($limit);
        }

        return $this->success($data);
    }
}
